package sk.fiit.vppj.eventmanager.volunteer;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import sk.fiit.vppj.eventmanager.core.ConfigManager;
import sk.fiit.vppj.eventmanager.data.Organization;
import sk.fiit.vppj.eventmanager.data.Participant;
import sk.fiit.vppj.eventmanager.data.Project;
import sk.fiit.vppj.eventmanager.data.Supervisor;
import sk.fiit.vppj.eventmanager.data.User;

final class VolunteerGeneralPanel extends JPanel {
	private JPanel tablePanel = new VolunteerGeneralTable( this );
	private JPanel personalPanel = null;
	private User user = null;
	private JLabel photoPortrait = null;
	private final static Logger log = Logger.getLogger( VolunteerGeneralPanel.class.getName() );

	public VolunteerGeneralPanel() {
		setLayout( new GridLayout( 1, 2 ) );
		add( tablePanel );
		this.personalPanel = new JPanel();
		this.personalPanel.setLayout( new BoxLayout( personalPanel, BoxLayout.PAGE_AXIS ) );
		add( this.personalPanel );

	}

	public void showPersonalData(User selectedPerson) {
		this.user = selectedPerson;

		this.personalPanel.removeAll();
		if( this.user == null ) {
			personalPanel.add( getTitle( "No data available!" ) );
		}
		else {
			refreshPersonalPanel();
		}
		this.personalPanel.updateUI();
	}

	private void refreshPersonalPanel() {

		personalPanel.add( getTitle( "Personal data" ) );
		
		photoPortrait = getPhotoPortrait( this.user.getPhotoPortrait() );
		personalPanel.add( photoPortrait );

		this.personalPanel.add( getTabbedMenu() );
	}

	private String getUserData() {
		StringBuilder text = new StringBuilder( 1000 );
		text.append( "<html>" );
		insertNameField( text );
		insertEmailField( text );
		insertClassField( text );
		insertSexField( text );
		insertAddressField( text );
		insertPhoneNumberField( text );
		insertEsiTripField( text );
		insertFuntripField( text );
		text.append( "</html>" );
		return text.toString();
	}

	private JLabel getTitle(String text) {
		JLabel title = new JLabel( text, JLabel.CENTER );
		title.setFont( new Font( "Arial", Font.PLAIN, 18 ) );

		return title;
	}

	// TODO refactor position of all included tabs
	private JPanel getTabbedMenu() {
		JPanel panel = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
		panel.setMaximumSize( new Dimension( 400, 400 ) );
		JTabbedPane tabbedPane = new JTabbedPane();

		tabbedPane.addTab( "General", new JLabel( getUserData() ) );

		if( this.user.getClazz().equals( "participant" ) ) {
			tabbedPane.addTab( "Projects", new JLabel( getProjectData( ( ( Participant ) this.user ).getProject() ) ) );
		}
		if( this.user.getClazz().equals( "participant" ) || this.user.getClazz().equals( "supervisor" ) ) {
			tabbedPane.addTab( "Organizations", new JLabel( getOrganizationData( this.user.getClazz() ) ) );
		}

		panel.add( tabbedPane );
		return panel;
	}

	// TODO refactor
	private String getProjectData(Project project) {
		StringBuilder text = new StringBuilder( 2000 );
		text.append( "<html>" );
		text.append( "<strong>Project Name: </strong>" );
		text.append( project.getEngName() );
		text.append( "<br>" );
		text.append( "<strong>Category: </strong>" );
		text.append( project.getCategoryName() );
		text.append( "<br>" );
		text.append( "<strong>English abstract: </strong>" );
		text.append( project.getEngAbstract() );
		text.append( "</html>" );
		return text.toString();
	}

	// TODO refactor
	private String getOrganizationData(String clazz) {
		Organization org = null;
		if( clazz.equals( "participant" ) ) {
			org = ( ( Participant ) this.user ).getOrganization();
		}
		else {
			org = ( ( Supervisor ) this.user ).getOrganization();
		}

		StringBuilder text = new StringBuilder( 1000 );
		text.append( "<html>" );
		text.append( "<div><strong>Organization Name: </strong>" );
		text.append( org.getName() );
		text.append( "</div>" );
		text.append( "<div><strong>Boss name: </strong>" );
		text.append( org.getBossName() );
		text.append( "</div>" );
		text.append( "<div><strong>State: </strong>" );
		text.append( org.getState() );
		text.append( "</div>" );
		text.append( "</html>" );
		return text.toString();
	}

	private void insertNameField(StringBuilder text) {
		text.append( "<div><strong>Name: </strong>" );
		text.append( this.user.getName() );
		text.append( "</div>" );
	}

	private void insertEmailField(StringBuilder text) {
		text.append( "<div><strong>Email: </strong>" );
		text.append( this.user.getEmail() );
		text.append( "</div>" );
	}

	private void insertClassField(StringBuilder text) {
		text.append( "<div><strong>Class: </strong>" );
		text.append( this.user.getClazz() );
		text.append( "</div>" );
	}

	private void insertSexField(StringBuilder text) {
		text.append( "<div><strong>Sex: </strong>" );
		text.append( this.user.getSex() );
		text.append( "</div>" );
	}

	private void insertAddressField(StringBuilder text) {
		text.append( "<div><strong>Address: </strong>" );
		text.append( this.user.getAddress() );
		text.append( ", " );
		text.append( this.user.getCity() );
		text.append( ", " );
		text.append( this.user.getZip() );
		text.append( ", " );
		text.append( this.user.getState() );
		text.append( "</div>" );
	}

	private void insertPhoneNumberField(StringBuilder text) {
		text.append( "<div><strong>Phone number: </strong>" );
		text.append( this.user.getPhoneNumber() );
		text.append( "</div>" );
	}

	private void insertEsiTripField(StringBuilder text) {
		text.append( "<div><strong>Esi trip: </strong>" );
		if( this.user.getEsiTour() == null ) {
			text.append( "No" );
		}
		else {
			text.append( this.user.getEsiTour().getName() );
		}
		text.append( "</div>" );
	}

	private void insertFuntripField(StringBuilder text) {
		text.append( "<div><strong>Fun trip: </strong>" );
		if( this.user.getFuntrip() == null ) {
			text.append( "No" );
		}
		else {
			text.append( this.user.getFuntrip().getName() );
		}
		text.append( "</div>" );
	}

		

	private JLabel getPhotoPortrait(String filename) {
		String photoLocation = new ConfigManager().getProperties().getProperty( "photoLocation", "" ).toString();
		JLabel photo = null;
		if( photoLocation == "" ) {
			photo = new JLabel( new ImageIcon( this.getClass().getResource( "/resources/no_image.gif" ) ) );
		}
		else {
			Image portrait = new ImageIcon( photoLocation + "\\" + filename ).getImage();
			Image image = portrait.getScaledInstance( 180, 240, Image.SCALE_SMOOTH );
			photo = new JLabel( new ImageIcon( image ) );
		}
		return photo;
	}
}
